<?php

namespace common\modules\wechat\models\backend;

use common\helpers\Html;
use common\modules\wechat\Module as WechatModule;
use common\widgets\grid\ActionColumn;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\data\ArrayDataProvider;

/**
 * ModuleSearch represents the model behind the search form about `common\modules\wechat\models\backend\Module`.
 */
class ModuleSearch extends Module
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'name', 'type', 'category', 'version', 'ability', 'description', 'author', 'site'], 'safe'],
            [['admin', 'migration', 'reply_rule', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/wechat', 'Module');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $dbModules = self::find()->indexBy('id')->all();

        $dataProvider = new ArrayDataProvider([
            'allModels' => array_merge(WechatModule::findAvailableModules(), $dbModules),
        ]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [

            [
                'attribute' => 'id',
                'filter' => false,
            ],

            [
                'attribute' => 'name',
                'filter' => false,
            ],

            [
                'attribute' => 'type',
                'width' => '150px',
                'filter' => false,
            ],

            [
                'attribute' => 'category',
                'width' => '150px',
                'filter' => false,
            ],

            [
                'attribute' => 'version',
                'filter' => false,
            ],

            [
                'attribute' => 'author',
                'filter' => false,
                'format' => 'html',
                'value' => function ($model) {
                    $author = $model->author ?: '匿名';
                    return $model->site ? Html::a($author, $model->site) : Html::encode($author) ;
                },
            ],

            [
                'attribute' => 'created_at',
                'filter' => false,
                'format'    => ['date', 'php:Y-m-d H:i:s'],
            ],

            [
                'attribute' => 'updated_at',
                'filter' => false,
                'format'    => ['date', 'php:Y-m-d H:i:s'],
            ],

            [
                'class' => ActionColumn::className(),
                'width' => '100px',
                'template' => '{install}',
                'buttons' => [
                    'install' => function ($url, $model, $key) {
                        /** @var static $model */
                        if ($model->getIsInstalled())
                        {
                            return Html::a('卸载模块', ['uninstall', 'id' => $model->id], ['data-confirm' => '确定要卸载此模块?']);
                        }
                        else
                        {
                            return Html::a('安装模块', ['install', 'id' => $model->id]);
                        }
                    }
                ]
            ],
        ];
    }
}
